package cn.lyz.authority.common.vo;

import cn.lyz.authority.common.enums.RespStatusEnum;
import lombok.Data;
import lombok.ToString;
import lombok.experimental.Accessors;

@Data
@ToString
@Accessors(chain = true)
public class LyzResponseBody<T> {
    /**
     * 响应代码
     */
    private int code;

    /**
     * 响应消息
     */
    private String msg;

    /**
     * 响应结果
     */
    private T data;

    private LyzResponseBody(T data){
        this.setData(data);
        this.code= RespStatusEnum.SUCCESS.getCode();
        this.msg = RespStatusEnum.SUCCESS.getMsg();
    }

    private LyzResponseBody(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    /**
     * 成功
     *
     */
    public static <T> LyzResponseBody<T> success() {
        return new LyzResponseBody<>(RespStatusEnum.SUCCESS.getCode(),RespStatusEnum.SUCCESS.getMsg());
    }

    /**
     * 成功
     */
    public static <T> LyzResponseBody<T> success(T data) {
        return new LyzResponseBody<>(data);
    }

    /**
     * 失败
     */
    public static <T> LyzResponseBody<T> error(Throwable error) {
        return new LyzResponseBody<>(RespStatusEnum.ERROR.getCode(),error.getMessage());
    }

    public static <T> LyzResponseBody<T> error(String message) {
        return new LyzResponseBody<>(RespStatusEnum.ERROR.getCode(),message);
    }
}
